package org.example.controller.supplier;

import org.example.entity.Supplier;
import org.example.service.SupplierService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/supplier/updateSupplierProcess")
public class updateSupplierProcessController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码为UTF-8
        req.setCharacterEncoding("UTF-8");
        //接收超链接传递过来的id值
        String id = req.getParameter("id");
//        获取前端表单提交的数据
        String name = req.getParameter("name");  //获取供应商名称输入,这里的参数名和表单里设计的name保持一致
        String addr = req.getParameter("addr");  //获取地址输入
        String phone = req.getParameter("phone");//获取电话输入
        //组装新的一个供应商实体
        Supplier supplier = new Supplier();
        supplier.setId(Integer.parseInt(id));
        supplier.setName(name);
        supplier.setAddress(addr);
        supplier.setTelephone(phone);
//        调用supplier类的实现类
        SupplierService supplierService = new SupplierService();
//        调用实现类的更新方法
        try {
            if(supplierService.updateSupplier(supplier)==1){
                PrintWriter out = resp.getWriter();
                out.print("<script>alert('success...'); window.location='/supplier' </script>");
                out.flush();
                out.close();
            }else{
                PrintWriter out = resp.getWriter();
                out.print("<script>alert('failure...'); window.location='/supplier' </script>");
                out.flush();
                out.close();
            }
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
